package exercise;

import javax.management.ObjectName;
import java.util.Scanner;

/*
b.在控制台输入字符串和子字符串，并计算字符串中子字符串 出现的次数
比如:输入字符串:"abcdfsdfabcdfabcdsf";
子字符串:"abc";
出现次数:3次
 */
public class ex2 {
    public static void main(String[] args) {
//        String str1 = "abcdfsdfabcdfabcdsf";
//        String str2 = "abc";
        Scanner sc = new Scanner(System.in);
        while (true) {
            System.out.print("请输入字符串:");
            String str1 = sc.next();
            System.out.print("请输入子字符串:");
            String str2 = sc.next();

            int count = getCount(str1, str2);
            System.out.println("count = " + count);
        }
    }

    private static int getCount(String str1, String str2) {
        int count = 0;
        int index = str1.indexOf(str2);
        while (index != -1 && index < str1.length()) {
            count++;
            index = str1.indexOf(str2, index +str2.length());
        }
        return count;
    }
}
